//
//  MeViewController.m
//  Molian
//
//  Created by 胡津 on 15/3/16.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//
#define chooseBtnTag 100

#import "MeViewController.h"
#import "TaskViewController.h"//奖励
#import "PrivilegeViewController.h"//特权
#import "SettingsViewController.h"//设置
#import "UserDataEditViewController.h"//资料编辑
#import "UIImage+StackBlur.h"//背景模糊
#import "CustomizeLoverViewController.h"//定制恋人

#import "ArticleViewController.h"
#import "MLUserDataViewController.h"
#import "MLTabBar.h"
#import "MLHomeViewController.h"
#import "WalletViewController.h"
#import "MeHeadCell.h"//头部view
#import "MeCell.h"//内容cell
#import "LikeUserViewController.h"
#import "LikeRecommendedViewController.h"
#import "LikeMeViewController.h"
#import "LikeViewController.h"
#import "PayViewController.h"
#import "RechargeViewController.h"
#import "MyGiftViewController.h"

@interface MeViewController ()<UITableViewDelegate,UITableViewDataSource>

//视图列表
@property (nonatomic, strong)UITableView *tableView;
//记录index
@property NSInteger chooseBtnIndex;

@property (nonatomic, strong)MLUser *currentUser;
@end

@implementation MeViewController
{
    NSArray *titleArr;
    NSArray *iconArr;
}

-(void)Settings
{
    [Utils pushTabbarHidden:[[SettingsViewController alloc]init] Selfvc:self];
}

#pragma mark - life cycle

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
   
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];//状态栏颜色
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:RGBA(31, 30, 30, 0.99)] forBarMetrics:UIBarMetricsDefault];
    
//    MLUser *user = [MLUser currentUser];
//    [user refreshInBackgroundWithBlock:^(AVObject *object, NSError *error) {
//        if (user.type != MLUserType_NORMAL) {
//            [[NSNotificationCenter defaultCenter] removeObserver:self name:@"cmd" object:nil];
//            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cmd:) name:@"cmd" object:nil];
//            [[NSNotificationCenter defaultCenter] removeObserver:self name:NOTIFICATION_TYPE_NEW_CUSTOMAZITION_PUSH object:nil];
//            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(customization) name:NOTIFICATION_TYPE_NEW_CUSTOMAZITION_PUSH object:nil];
//            if([Utils cheackConfigurationEmotionalHotline]){
//                titleArr = @[@[@""],@[@"钱包",@"奖励",@"礼物"],@[@"喜欢我的",@"我喜欢的"],@[@"情感热线",@"评价"]];
//                iconArr = @[@[@""],@[@"mine_wallet_icon",@"mine_task_icon",@"gifticon"],@[@"icon_like_me",@"mine_-like-recommend_icon"],@[@"mine_privileges_icon",@"mine_--invitation_icon"]];
//            }else{
//                titleArr = @[@[@""],@[@"钱包",@"奖励",@"礼物"],@[@"喜欢我的",@"我喜欢的"],@[@"评价"]];
//                iconArr = @[@[@""],@[@"mine_wallet_icon",@"mine_task_icon",@"gifticon"],@[@"icon_like_me",@"mine_-like-recommend_icon"],@[@"mine_--invitation_icon"]];
//            }
//        }else{
//           
//        }
//        [self.tableView reloadData];
//    }];
    
    titleArr = @[@[@""],@[@"钱包",@"奖励",@"礼物"],@[@"喜欢我的",@"我喜欢的"]];
    iconArr = @[@[@""],@[@"mine_wallet_icon",@"mine_task_icon",@"gifticon"],@[@"icon_like_me",@"mine_-like-recommend_icon"]];
    

    
    self.currentUser = [MLUser currentUser];
    [self.tableView reloadData];
    
}

- (void)customization
{
//    if (!newView) {
//        CGRect rectInTableView = [self.tableView rectForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:2]];
//        UIView*viewCell = [[UIView alloc]initWithFrame:rectInTableView];
//        viewCell.userInteractionEnabled = NO;
//        [self.tableView addSubview:viewCell];
//        
//        //角标
//        newView = [[UIView alloc]initWithFrame:CGRectMake(DeviceWidth-60, (50-7.5)/2, 7.5, 7.5)];
//        newView.layer.masksToBounds = YES;//开启圆角
//        [newView.layer setCornerRadius:7.5/2];
//        newView.backgroundColor = UIColorFromRGB(0xFE6A66);
//        newView.alpha = 0;
//        [viewCell addSubview:newView];
//    }
//    [Utils Hidden:newView Bool:YES];
}
-(void)cmd:(NSNotification *)notification
{
    NSDictionary *dic = [notification userInfo];
    EMMessage *message = [dic objectForKey:@"message"];
    EMCommandMessageBody *body= message.messageBodies.lastObject;
    
    if ([message.from isEqualToString:@"new_liker"]){
       //喜欢我的
        NSIndexPath *indexpath = [NSIndexPath indexPathForItem:0 inSection:2];
        MeCell *cell = (MeCell *)[self.tableView cellForRowAtIndexPath:indexpath];
        if (cell) {
            cell.redDotView.hidden = NO;
        }
        
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.∂
    self.title = @"我";
    self.view.backgroundColor = APPCOLORBACKGROUNDCOLOR(1);
    self.currentUser = [MLUser currentUser];
    UIBarButtonItem *rightItem  = [[UIBarButtonItem alloc]initWithTitle:@"更多" style:UIBarButtonItemStylePlain target:self action:@selector(Settings)];
    self.navigationItem.rightBarButtonItem = rightItem;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavigationBarHeight, DeviceWidth, DeviceHeight-kNavigationBarHeight-50)style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollsToTop = YES;
    self.tableView.separatorColor = RGBA(225, 225, 225, 1);//分隔条颜色
//    self.tableView.contentInset = UIEdgeInsetsMake(110, 0, 0, 0);//偏移量
    self.tableView.tableFooterView = [[UIView alloc] init];//不显示空的cell
    [self.view addSubview:self.tableView];
    
   
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cmd:) name:@"cmd" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(customization) name:NOTIFICATION_TYPE_NEW_CUSTOMAZITION_PUSH object:nil];
}


#pragma mark - tableview delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MeCell *cell;
    MeHeadCell *headcell;
    if (indexPath.section == 0) {
        static  NSString *CellIdentifier = @"headcell";
        headcell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if  (headcell == nil) {
            headcell = [[MeHeadCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        }
        if (!self.currentUser.subAvatar || [self.currentUser.subAvatar isEqualToString:@""]) {
            [headcell.userAvatarImg sd_setImageWithURL:[NSURL URLWithString:self.currentUser.avatar.url] placeholderImage:[UIImage imageNamed:@"common_avatar_loading_background"]];
            
        }else{
            [headcell.userAvatarImg sd_setImageWithURL:[NSURL URLWithString:self.currentUser.subAvatar] placeholderImage:[UIImage imageNamed:@"common_avatar_loading_background"]];
        }
        //头像
       // [headcell.userAvatarImg sd_setImageWithURL:[NSURL URLWithString:self.currentUser.avatar.url]];
        //名字
        
        headcell.userName.text = self.currentUser.nickname;
        if ([Utils getUserDataIntegrityNumber]) {
             headcell.integrityLabel.hidden = NO;
            headcell.integrityLabel.text = [NSString stringWithFormat:@"资料完整度%ld%%",(long)[Utils getUserDataIntegrityNumber]];
        }else{
            headcell.integrityLabel.hidden = YES;
        }
        
        headcell.daihao.text = [NSString stringWithFormat:@"袋号:%ld",(long)self.currentUser.molianId];
        headcell.backgroundColor =[UIColor whiteColor];
        return headcell;
    }else{
        static  NSString *CellIdentifier = @"cell";
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if  (cell == nil) {
            cell = [[MeCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        }
        NSString *title = [[titleArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        
        
        if ([title isEqualToString:@"奖励"]) {
            //奖励
            if (![[Utils defaultsForKey:@"jiangli"]isEqualToString:@"1"]) {
                cell.redDotView.hidden = NO;
            }else{
                cell.redDotView.hidden = YES;
            }
        }else if([title isEqualToString:@"喜欢我的"]){
            if ([[Utils defaultsForKey:@"new_liker"]isEqualToString:@"YES"]) {
                cell.redDotView.hidden = NO;
            }else{
                cell.redDotView.hidden = YES;
            }
        }else{
            cell.redDotView.hidden = YES;
        }
        
       
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 50, 0, 0)];//分割线
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//cell的右边有一个小箭头，距离右边有十几像素；
        
        cell.TitleLabel.text = title;
        cell.IcoView.image = [UIImage imageNamed:[[iconArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]];
        return cell;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section==0){
        MLUserDataViewController *userInfoVC = [[MLUserDataViewController alloc]init];
        userInfoVC.userData = self.currentUser;
        [Utils pushTabbarHidden:userInfoVC Selfvc:self];
        
    }else{
         NSString *title = [[titleArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        if([title isEqualToString:@"钱包"]){
            [Utils pushTabbarHidden:[[WalletViewController alloc]init] Selfvc:self];
            
        }else if([title isEqualToString:@"奖励"]){
            MeCell *cell = (MeCell *)[tableView cellForRowAtIndexPath:indexPath];
            cell.redDotView.hidden = YES;
            [Utils saveToDefaults:@"1" forKey:@"jiangli"];
            [Utils pushTabbarHidden:[[TaskViewController alloc]init] Selfvc:self];
            
        }else if([title isEqualToString:@"礼物"]){
            MyGiftViewController *giftVC = [[MyGiftViewController alloc] init];
            //礼物记录
            [Utils pushTabbarHidden:giftVC Selfvc:self];
            
        }else if([title isEqualToString:@"喜欢我的"]){
            MeCell *cell = (MeCell *)[tableView cellForRowAtIndexPath:indexPath];
            cell.redDotView.hidden = YES;
            [Utils saveToDefaults:@"NO" forKey:@"new_liker"];
            [Utils pushTabbarHidden:[[LikeMeViewController alloc] init] Selfvc:self];
            
        }else if([title isEqualToString:@"我喜欢的"]){
            [Utils pushTabbarHidden:[[LikeViewController alloc] init] Selfvc:self];
        }
        
    }
        
}

//section个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [titleArr count];
}

//cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[titleArr objectAtIndex:section] count];//Cell个数
}

//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 80;
    }else{
        return 50;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;//每个cell的头部间距

}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
{
    return 10;//每个cell的底部间距
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
}

@end
